package com.redstar.HappyRefresh.util;

import org.joda.time.DateTime;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {

    private static final String dateFormat = "yyyy-MM-dd";

    public static String formatDate(Date date) { // 格式化日期

        SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
        return sdf.format(date);

    }

    public static Date addDateSeconds(Date date, int seconds) { // 对日期的秒进行加或减

        DateTime dateTime = new DateTime(date);
        return dateTime.plusSeconds(seconds).toDate();

    }

    public static Date addDateMinutes(Date date, int minutes) { // 对日期的分钟进行加或减

        DateTime dateTime = new DateTime(date);
        return dateTime.plusMinutes(minutes).toDate();

    }

    public static Date addDateHours(Date date, int hours) { // 对日期的小时进行加或减

        DateTime dateTime = new DateTime(date);
        return dateTime.plusHours(hours).toDate();

    }

    public static Date addDateDays(Date date, int days) { // 对日期的天进行加或减

        DateTime dateTime = new DateTime(date);
        return dateTime.plusDays(days).toDate();

    }

    public static Date addDateWeeks(Date date, int weeks) {// 对日期的周进行加或减

        DateTime dateTime = new DateTime(date);
        return dateTime.plusWeeks(weeks).toDate();

    }

    public static Date addDateMonths(Date date, int months) { // 对日期的月进行加或减

        DateTime dateTime = new DateTime(date);
        return dateTime.plusMonths(months).toDate();

    }

    public static Date addDateYears(Date date, int years) { // 对日期的年进行加或减

        DateTime dateTime = new DateTime(date);
        return dateTime.plusYears(years).toDate();

    }

    public static boolean isLastDayOfMonth(Date date) { // 判断给定日期是否是当月的最后一天

        //1、创建日历类
        Calendar calendar = Calendar.getInstance();
        //2、设置当前传递的时间，不设就是当前系统日期
        calendar.setTime(date);
        //3、data的日期是N，那么N+1,假设当月是30天，30+1=31，如果当月只有30天，那么最终结果为1，也就是下月的1号
        calendar.set(Calendar.DATE, (calendar.get(Calendar.DATE) + 1));
        //4、判断是否是当月最后一天【1==1那么就表明当天是当月的最后一天返回true】
        return calendar.get(Calendar.DAY_OF_MONTH) == 1;

    }

    public static String getLastMonth() { // 获取上个月月份

        SimpleDateFormat format = new SimpleDateFormat("yyyyMM");
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        // 设置为当前时间
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, -1);
        // 设置为上一个月
        //calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1);
        date = calendar.getTime();
        return format.format(date);

    }

    public static String getLastMonth(String repeatDate) { // 获取任意时间的上一个月

        String lastMonth = "";
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat dft = new SimpleDateFormat("yyyyMM");
        int year = Integer.parseInt(repeatDate.substring(0, 4));
        String monthsString = repeatDate.substring(4, 6);
        int month;
        if ("0".equals(monthsString.substring(0, 1))) {
            month = Integer.parseInt(monthsString.substring(1, 2));
        } else {
            month = Integer.parseInt(monthsString.substring(0, 2));
        }
        cal.set(year, (month - 2), Calendar.DATE);
        lastMonth = dft.format(cal.getTime());
        return lastMonth;

    }
}